<template>
  <!-- 新增部门的弹层 -->
  <el-dialog :title="showTitle" :visible="isShowAddDept" @open="getEmployeeSimple" @close="btnCancel">
    <!-- 表单组件  el-form   label-width设置label的宽度   -->
    <!-- 匿名插槽 -->
    <el-form ref="form" :model="formData" :rules="rules" label-width="120px">
      <el-form-item label="部门名称" prop="name">
        <el-input
          v-model="formData.name"
          style="width:80%"
          placeholder="1-50个字符"
        />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input
          v-model="formData.code"
          style="width:80%"
          placeholder="1-50个字符"
        />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select
          v-model="formData.manager"
          style="width:80%"
          placeholder="请选择"
        >
          <!-- 需要循环生成选项   这里做一下简单的处理 显示的是用户名 存的也是用户名-->
          <el-option
            v-for="item in peoples"
            :key="item.id"
            :label="item.username"
            :value="item.username"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input
          v-model="formData.introduce"
          style="width:80%"
          placeholder="1-300个字符"
          type="textarea"
          :rows="3"
        />
      </el-form-item>
    </el-form>
    <!-- el-dialog有专门放置底部操作栏的 插槽  具名插槽 -->
    <el-row slot="footer" type="flex" justify="center">
      <!-- 列被分为24 -->
      <el-col :span="6">
        <el-button
          type="primary"
          size="small"
          @click="btnOK"
        >确定</el-button>
        <el-button
          size="small"
          @click="btnCancel"
        >取消</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
export default {
  props: {
    isShowAddDept: {
      type: Boolean,
      default: false
    },
    treeNode: {
      type: Object,
      default: null
    }
  },
  data() {
    const checkNameRepeat = async(rule, value, callback) => {
      let isNameRepeat = false
      // 获取最新的组织架构
      const { depts } = await this.apis.departments.getDepartments()
      // 判断是编辑还是新增
      if (this.formData.id) {
        isNameRepeat = depts.filter(item => item.id !== this.formData.id && item.pid === this.treeNode.pid).some(obj => obj.name === value)
      } else {
        // 判断同级的组织架构中是否有名字相同的部门
        isNameRepeat = depts.filter(item => item.pid === this.treeNode.id).some(obj => obj.name === value)
      }
      isNameRepeat ? callback(new Error(`同级部门下已经有${value}的部门了`)) : callback()
    }
    const checkCodeRepeat = async(rule, value, callback) => {
      let isCodeRepeat = false
      // 获取最新的组织架构
      const { depts } = await this.apis.departments.getDepartments()
      // 判断同级的组织架构中是否有名字相同的部门
      if (this.formData.id) {
        isCodeRepeat = depts.filter(item => item.id !== this.formData.id).some(obj => obj.code === value)
      } else {
        isCodeRepeat = depts.some(obj => obj.code === value)
      }
      isCodeRepeat ? callback(new Error(`组织架构中已经有部门使用${value}编码`)) : callback()
    }
    return {
      formData: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      },
      rules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门名称要求1-50个字符', trigger: 'blur' },
          { validator: checkNameRepeat, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门编码要求1-50个字符', trigger: 'blur' },
          { validator: checkCodeRepeat, trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '部门负责人不能为空', trigger: ['blur', 'change'] }
        ],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: 'blur' },
          { trigger: 'blur', min: 1, max: 300, message: '部门介绍要求1-50个字符' }
        ]
      },
      peoples: null
    }
  },
  computed: {
    showTitle() {
      return this.formData.id ? '编辑部门' : '新增子部门'
    }
  },
  methods: {
    async getDepartDetail(id) {
      this.formData = await this.apis.departments.getDepartDetail(id)
      // console.log(res)
    },
    async getEmployeeSimple() {
      this.peoples = await this.apis.employees.getEmployeeSimple()
    },
    btnOK() {
      this.$refs.form.validate(async isOK => {
        // 校验成功
        if (isOK) {
          // 区分是编辑还是新增
          if (this.formData.id) {
            // 编辑
            await this.apis.departments.updateDepartments(this.formData)
            this.$message.success('编辑部门成功')
            // 提示父组件更新数据
            this.$emit('update-addDept')
          } else {
            // 新增
            await this.apis.departments.addDepartments({ ...this.formData, pid: this.treeNode.id })
            // 提示添加子部门成功
            this.$message.success('添加子部门成功')
            // 提示父组件更新数据
            this.$emit('update-addDept')
          }
          // 关闭弹出层
          this.$emit('update:isShowAddDept', false)
        }
      })
    },
    btnCancel() {
      // 重置数据  因为resetFields 只能重置 表单上的数据 非表单上的 比如 编辑中id 不能重置
      this.formData = {
        name: '',
        code: '',
        manager: '',
        introduce: ''
      }
      // 清空数据,看文档发现el-form有个重置方法
      this.$emit('update:isShowAddDept', false)
      this.$refs.form.resetFields()
    }
  }
}
</script>
